package com.tang.fangyi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Base64;

public class MyWindow extends JFrame implements ActionListener {
    private JTextField textField;
    private JTextArea textArea;
    private JButton decodedButton;
    private JButton encodedButton;
    private StringBuilder str;

    public MyWindow() {
        super("嘿嘿嘿");
        setResizable(true);
        setVisible(true);
        setBounds(0,0,500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建输入文本框和按钮
        textField = new JTextField(30);
        textField.setFont(new Font("SimHei", Font.PLAIN, 15));
        textField.setForeground(Color.black);
        textField.setPreferredSize(new Dimension(80, 50));
        //textField.setSize(50,50);
        encodedButton = new JButton("encoded");
        encodedButton.addActionListener(this);
        decodedButton = new JButton("decoded");
        decodedButton.addActionListener(this);


        // 创建结果标签
        textArea = new JTextArea(5, 30);
        textArea.setFont(new Font("SimHei", Font.BOLD, 20));
        textArea.setForeground(Color.RED);
        textArea.setLineWrap(true); // 设置自动换行
        textArea.setWrapStyleWord(true); // 设置按单词换行
        // 将 JTextArea 组件添加到 JScrollPane 中
        JScrollPane scrollPane = new JScrollPane(textArea);

        // 将组件添加到窗口中
        JPanel panel = new JPanel();
        panel.add(textField);
        panel.add(encodedButton);
        panel.add(decodedButton);
        getContentPane().add(panel, BorderLayout.NORTH);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 获取输入文本框中的内容
        String input = textField.getText();

        String str = "";
        if (e.getSource() == encodedButton) {
             str = Base64.getEncoder().encodeToString(input.getBytes());
            // 处理按钮1的点击事件
            //JOptionPane.showMessageDialog(this, "");//弹窗
        } else if (e.getSource() == decodedButton) {
            byte[] decodedBytes = Base64.getDecoder().decode(input);
             str = new String(decodedBytes);
            // 处理按钮2的点击事件
            //JOptionPane.showMessageDialog(this, "Button 2 clicked!");
        }

        // 在结果标签中显示输入内容
        textArea.setText(str);
        textField.setText("");
    }

    public static void main(String[] args) {
        MyWindow window = new MyWindow();
        window.setVisible(true);

    }

}
